import {
  Module,
  // ValidationPipe
} from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CoffeesModule } from './coffees/coffees.module';
import { CoffeeRatingModule } from './coffee-rating/coffee-rating.module';
import { DatabaseModule } from './database/database.module';
import { ConfigModule } from '@nestjs/config';
import { CommonModule } from './common/common.module';
// import * as Joi from '@hapi/joi';
// import appConfig from './config/app.config';

@Module({
  imports: [
    // 异步解决了依赖配置的问题
    // https://docs.nestjs.com/techniques/database#async-configuration
    TypeOrmModule.forRootAsync({
      // imports: [ConfigModule],
      // inject: [ConfigService],
      // dataSourceFactory: () => {},
      useFactory: () =>
        // useFactory: (configService: ConfigService) =>
        {
          // console.log(configService.get())
          return {
            type: 'postgres',
            host: process.env.DB_HOST,
            port: +process.env.DB_PORT,
            username: process.env.DB_USER,
            password: process.env.DB_PASSWORD,
            database: process.env.DB_NAME,
            synchronize: true, // 确保typeorm 实体，每次运行应用程序时都会与数据库同步
            autoLoadEntities: true, // 自动加载模块，而不是指定实体数组
          };
        },
    }),
    ConfigModule.forRoot({
      // // envFilePath: '.environment',
      // // ignoreEnvFile: true,
      // load: [appConfig],
      // validationSchema: Joi.object({
      //   DB_HOST: Joi.required(),
      //   DB_PORT: Joi.number().default(5432),
      // }),
    }),
    CoffeesModule,
    CoffeeRatingModule,
    DatabaseModule,
    CommonModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
